package com.sam.mvp.ui.holder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseHolder;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.sam.R;
import com.sam.mvp.entity.User;


import butterknife.BindView;
import io.reactivex.Observable;

public class UserHolder extends BaseHolder<User> {


    @BindView(R.id.iv_avatar)
    ImageView mAvatar;
    @BindView(R.id.tv_name)
    TextView mName;

    AppComponent appComponent;
    ImageLoader imageLoader;

    public UserHolder(View itemView) {
        super(itemView);
        appComponent = ArmsUtils.obtainAppComponentFromContext(itemView.getContext());
        imageLoader = appComponent.imageLoader();

    }

    @Override
    public void setData(User data, int position) {
        Observable.just(data.login).subscribe(name -> mName.setText(name));
        imageLoader.loadImage(itemView.getContext(),ImageConfigImpl.builder().url(data.avatar_url).imageView(mAvatar).build());
    }

    @Override
    protected void onRelease() {
        imageLoader.clear(appComponent.application(), ImageConfigImpl.builder()
                .imageViews(mAvatar)
                .build());
        super.onRelease();
        appComponent = null;
        imageLoader = null;
        mName = null;
        mAvatar = null;
    }
}
